/* $RCSFile: LegacyRule.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.RewriteRules/LegacyRule.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:30  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.Configuration;
using System.Globalization;
using System.Text;
using System.Web;
using System.Web.Configuration;

using AddonNice.RewriteRules.Tools;
using Rewrite.NET;

namespace AddonNice.RewriteRules
{
	/// <summary>
	/// This is a legacy Rule which supports all of the functionalities
	/// </summary>
	public class LegacyRule: IRules
	{

		public	bool BreakAfter
		{
			get
			{
				return Section.BreakOnAfter;
			}
            set {}
	    }

		public	bool BreakOnFirst
		{
			get
			{
				return Section.BreakOnFirst;
			}
            set {}
	    }

		IndexRule _Section	=	null;
		public IndexRule Section
		{
			get
			{
				return _Section;
			}
			set
			{
				_Section	=	value;
			}
		}
      
		public bool Execute(HttpApplication Appl,ref string Path)
        {
		    if ( RewriteTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]LegacyRule Execute BEG BreakOnFirst: {0}, BreakAfter: {1}, Path: '{2}'",BreakOnFirst,BreakAfter,Path),RewriteTraceSwitch.Sw.Info);
			string _handlerFlag     =   "go";
			bool _keywordSplitter   =   false;
			bool _aliasinurl        =   true;
			
			// collect settings
			string ob1				=	Section.HandlerFlag;
			if ( !string.IsNullOrEmpty(ob1))
			{
				_handlerFlag		=   ob1.ToLower(CultureInfo.InvariantCulture);
			}
			
			_keywordSplitter		=	Section.KeywordSplitter;

			_aliasinurl             =	Section.AliasInUrl;

			//determine if the user is navigating to a url to be handled
			string _startpath       =   (Engine.ApplicationPath(Appl).ToLower(CultureInfo.InvariantCulture) + "/" + _handlerFlag + "/");
			string path             =   Path;
			if ( RewriteTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]LegacyRule Path: {0}, _startpath: {1}", Path,_startpath),RewriteTraceSwitch.Sw.Info);
			if ( !path.ToLower(CultureInfo.InvariantCulture).StartsWith(_startpath) )
            {
                if (RewriteTraceSwitch.Sw.TraceVerbose)
                    Trace.WriteLine("[V]LegacyRule Execute END no match", RewriteTraceSwitch.Sw.Info);
                return false;
            }
			//Remove start tag...
			path			        =   path.Substring(_startpath.Length);
           
			//split the path into components
			string[] parts	        =   path.Split('/');

			StringBuilder sb        = new StringBuilder(Engine.ApplicationPath(Appl).ToString());
			sb.Append("/");
			sb.Append(Tools.General.DefaultPage);

			if (_aliasinurl)
			{
				General.AddAttribute(parts, sb, 0, "?Alias=");
				General.AddAttribute(parts, sb, 1, "&pageid=");
				General.AddAttribute(parts, sb, 2, "&lang=");
				if (_keywordSplitter)
					General.KeywordSplitter(parts, sb, 3);
				else
					General.AddAttribute(parts, sb, 3, "&");
			}
			else
            {
				General.AddAttribute(parts, sb, 0, "?pageid=");
				General.AddAttribute(parts, sb, 1, "&lang=");
				if (_keywordSplitter)
					General.KeywordSplitter(parts, sb, 2);
				else
					General.AddAttribute(parts, sb, 2, "&");
			}
            Path    =   sb.ToString();
            return true;
        }
    }
}

